<template lang="">
    <div>
     <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
       <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          :immediate-check="false"
          @load="onLoad">
          <ArticleItem v-for="obj in articleList" :key="obj.art_id" :article="obj" :isShowCorss="isShowCorss" @click.native="enterDetail(obj.art_id)"/>
          </van-list>
     </van-pull-refresh>
    </div>
</template>
<script>
import ArticleItem from './ArticleItem.vue'
export default {
  props: ['channelId'],
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      isLoading: false,
      // 通过时间戳来进行每一页的请求
      timeNow: new Date().getTime(),
      isShowCorss: true
    }
  },
  components: {
    ArticleItem
  },
  async created () {
    this.getData()
  },
  methods: {
    // 底部加载，异步请求
    async  onLoad () {
      if (this.articleList.length > 0) {
        this.getData()
      }
    },
    // 上拉刷新
    async onRefresh () {
      this.articleList = []
      this.timeNow = new Date().getTime()
      this.getData()
    },
    // 异步请求函数
    async getData () {
      const res = await this.$API.reqGetArticels(this.channelId, this.timeNow)
      const resList = res.data.data.results
      this.timeNow = res.data.data.pre_timestamp
      this.articleList = [...this.articleList, ...resList]
      this.loading = false
      this.isLoading = false
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
    },
    enterDetail (id) {
      this.$router.push({ path: '/articleDetail', query: { id } })
    }
  }
}
</script>
<style lang="">

</style>
